//
//  TSCellSelectCollectionView.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 2017/9/11.
//  Copyright © 2017年 dvlproad. All rights reserved.
//

#import "TSCellSelectCollectionView.h"
#import <CJCollectionViewLayout/CQCollectionViewFlowLayout.h>
#import <UICollectionViewCJHelper/UICollectionView+CJSelect.h>
#import "CQFilesLookCollectionViewCell.h"
#import <SDWebImage/SDWebImage.h>

@interface TSCellSelectCollectionView () <UICollectionViewDataSource> {
    
}

@end


@implementation TSCellSelectCollectionView


/// 初始化方法
- (instancetype)init {
    CQCollectionViewFlowLayout *layout = [[CQCollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //以下值必须二选一设置（默认cellWidthFromFixedWidth设置后，另外一个自动失效）
    layout.cellWidthFromPerRowMaxShowCount = 4;
    //layout.cellWidthFromFixedWidth = 50;
    layout.widthHeightRatio = 1.0;
    
    
    self = [super initWithFrame:CGRectZero collectionViewLayout:layout];
    if (self) {
        [self setupConfigure];
    }
    return self;
}
- (void)setupConfigure {
    self.backgroundColor = [UIColor clearColor];
    
    //以下值，可选设置
    //self.allowsMultipleSelection = YES; //是否打开多选
    
    /* 设置Register的Cell或Nib */
    CQFilesLookCollectionViewCell *registerCell = [[CQFilesLookCollectionViewCell alloc] init];
    [self registerClass:[registerCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    /* 设置DataSource */
    self.dataSource = self;
    
    /* 设置Delegate */
    self.delegate = self;
    
    
    self.isChoosing = NO;
}



#pragma mark - Setter
- (void)setIsChoosing:(BOOL)isChoosing {
    _isChoosing = isChoosing;
    
    [self reloadData];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger cellCount = self.dataModels.count;
    return cellCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CQFilesLookCollectionViewCell *dataCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    NSLog(@"dataCell.selected = %@", dataCell.selected ? @"YES" : @"NO");
    [self __operateDataCell:dataCell withIndexPath:indexPath isSettingOperate:YES];
    
    return dataCell;
}

#pragma mark - UICollectionViewDelegate
////“点到”item时候执行的时间(allowsMultipleSelection为默认的NO的时候，只有选中，而为YES的时候有选中和取消选中两种操作)
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.isChoosing) {
        //NSLog(@"选中");
        __weak typeof(self)weakSelf = self;
        [collectionView my_didTapItemAtIndexPath:indexPath
                        withAlwaysAloneIndexPath:self.alwaysAloneIndexPath
                             thisCellUpdateBlock:^(UICollectionView *collectionView, NSIndexPath *indexPath) {
            UICollectionViewCell *dataCell = [self cellForItemAtIndexPath:indexPath];
            [weakSelf __operateDataCell:dataCell withIndexPath:indexPath isSettingOperate:NO];
        }];
    } else {
        NSLog(@"浏览");
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *indexPathString = [NSString stringWithFormat:@"%zd-%zd", indexPath.section, indexPath.item];
    NSLog(@"取消点击%", indexPathString);
}



#pragma mark - Private Method
/*
 *  设置或者更新Cell
 *
 *  @param cell             要设置或者更新的Cell
 *  @param indexPath        要设置或者更新的Cell的indexPath
 *  @param isSettingOperate 是否是设置，如果否则为更新
 */
- (void)__operateDataCell:(CQFilesLookCollectionViewCell *)dataCell
            withIndexPath:(NSIndexPath *)indexPath
         isSettingOperate:(BOOL)isSettingOperate
{
    CQTSNetImageDataModel *dataModel = [self.dataModels objectAtIndex:indexPath.item];
    
    if (isSettingOperate) {
        dataCell.cjTextLabel.text = dataModel.name;
        
//        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dataModel.imageUrl]]];
//
//            dispatch_async(dispatch_get_main_queue(), ^{
//                dataCell.cjImageView.image = image;
//            });
//        });
        [dataCell.cjImageView sd_setImageWithURL:[NSURL URLWithString:dataModel.imageUrl] completed:nil];
        
        dataCell.cjSelectedButton.hidden = !self.isChoosing;
    }
}


@end
